package com.ysx.student.fangyahao.day20210910.studentmanagement.dao;

import com.ysx.student.fangyahao.day20210910.studentmanagement.entity.ScoreEntity;
import com.ysx.student.fangyahao.day20210910.studentmanagement.entity.StundentEntity;


import java.util.ArrayList;

public class StudentDao {

    private ArrayList<StundentEntity> arrayList = new ArrayList<StundentEntity>();

    public ArrayList<StundentEntity> getArrayList() {
        return arrayList;
    }

    public boolean addStudent(StundentEntity stundentEntity){
        return arrayList.add(stundentEntity);
    }

    public boolean matchStudent(String name,String password){
        for(int index=0;index<arrayList.size();index++){
            if(name.equals(arrayList.get(index).getUsername())&&password.equals(arrayList.get(index).getPassword())){
               return true;
            }
        }
        return false;
    }

    public int searchStudent(String userName){
        int flag=-1;
        for(int index=0;index<arrayList.size();index++){
            if(userName.equals(arrayList.get(index).getUsername())){
                flag = index;
            }
        }
        return flag;
    }

    public boolean deleteStudent(String userName){
        arrayList.remove(searchStudent(userName));
        return true;
    }

    public boolean addScore(String userName, ScoreEntity score){
        StundentEntity stundent = arrayList.get(searchStudent(userName));
        stundent.setScore(score);
        return true;
    }

    public boolean changeMathsScore(String userName, int score){
        ScoreEntity scoreEntity =arrayList.get(searchStudent(userName)).getScore();
        scoreEntity.setMaths(score);
        arrayList.get(searchStudent(userName)).setScore(scoreEntity);
        return true;
    }

    public boolean printAllStudent(){
        for(int index=0;index<arrayList.size();index++){
            System.out.println("姓名:"+arrayList.get(index).getUsername()+"密码:"+arrayList.get(index).getPassword());
        }
        return true;
    }

    public StundentEntity getStudentByMaxScoreOfMaths(){
        int max =0;
        for(int index=1;index<arrayList.size();index++){
            if(arrayList.get(index).getScore()==null){
                continue;
            }
            if(arrayList.get(max).getScore().getMaths()<arrayList.get(index).getScore().getMaths()){
                max=index;
            }
        }
        return arrayList.get(max);
    }

    public int getStudentAverage(int index){
        int sum = arrayList.get(index).getScore().getMaths()+arrayList.get(index).getScore().getChinese()+arrayList.get(index).getScore().getEnglish();
        int average =sum/3;
        return average;
    }

    public StundentEntity getStudentByMaxAverage(){
        int max =0;
        for(int index=1;index<arrayList.size();index++){
            if(arrayList.get(index).getScore()==null){
                continue;
            }
            if(getStudentAverage(max)<getStudentAverage(index)){
                max=index;
            }
        }
        return arrayList.get(max);
    }



}
